package com.yonyou.pmclouds.repeatwork.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.copyperson.entity.CopyPersonVO;
import com.yonyou.pmclouds.copyperson.rmiitf.ICopyPersonQuery;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.pmpub.entity.QueryListParam;
import com.yonyou.pmclouds.pmpub.rmiitf.IPageQuerySerivce;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.repeatwork.entity.RepeatWorkVO;
import com.yonyou.pmclouds.repeatwork.mapper.RepeatWorkMapper;
import com.yonyou.pmclouds.repeatwork.service.rmiitf.IRepeatWorkQuery;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = IRepeatWorkQuery.class)
public class RepeatWorkQueryImpl implements IRepeatWorkQuery, IPageQuerySerivce<RepeatWorkVO> {
    @Autowired
    private RepeatWorkMapper repeatWorkMapper;
    @Autowired
    private PictureRemoteService picService;
    @Autowired
    private ICopyPersonQuery copyPersonQuery;

    @Override
    public RepeatWorkVO[] queryByProject(QueryListParam queryListParam) throws BusinessException {
        RepeatWorkVO[] vos =repeatWorkMapper.queryByCondition(new String[]{queryListParam.getPkProject()}, null,  queryListParam.getStartDate(), queryListParam.getEndDate(), null,queryListParam.getPageStart(),queryListParam.getPageSize());
        if(null == vos || vos.length == 0){
            return new RepeatWorkVO[0];
        }


        return vos;
    }

    @Override
    public int countByProject(QueryListParam queryListParam) throws BusinessException {
        return 0;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.REPEATWORK_BILLTYPE;
    }

    @Override
    public RepeatWorkVO queryOneByPk(String pkRepeatwork) throws BusinessException {
        RepeatWorkVO repeatWorkVO = repeatWorkMapper.selectByPrimaryKey(pkRepeatwork);
        return repeatWorkVO;
    }

    @Override
    public RepeatWorkVO queryByProject(String pkProject) throws BusinessException {

        return null;
    }

    @Override
    public RepeatWorkVO getReport(String reportId) throws BusinessException {
        String[] pics = picService.queryPicNamesByPkEntity(reportId);
        RepeatWorkVO repeatWorkVO = repeatWorkMapper.selectByPrimaryKey(reportId);
        repeatWorkVO.setPictures(pics);

        if(repeatWorkVO.getBillStatus() !=null){
            switch (repeatWorkVO.getBillStatus()) {
                case BillStatusConst.COMMON_APPROVING_STATUS:
                    repeatWorkVO.setStatus("审批中");
                    break;
                case BillStatusConst.COMMON_REJECT_STATUS:
                    repeatWorkVO.setStatus("驳回");
                    break;
                case BillStatusConst.COMMON_APPROVED_STATUS:
                    repeatWorkVO.setStatus("审批通过");
                    break;
                case BillStatusConst.COMMON_COMMIT_STATUS:
                    repeatWorkVO.setStatus("驳回后再提交");
                    break;
                case BillStatusConst.COMMON_FREE_STATUS:
                    repeatWorkVO.setStatus("自由态");
            }
        }

        List<String> list = new ArrayList<>();
        list.add(reportId);
        List<CopyPersonVO> copyPersonVOS = copyPersonQuery.queryByPkBills(list);
        if(null != copyPersonVOS && copyPersonVOS.size() > 0) {
            repeatWorkVO.setCopyPersonVOList(copyPersonVOS);
        }

        String pkProject = repeatWorkVO.getPkProject();
        ProjectVO projectVO = repeatWorkMapper.selectProject(pkProject);
        repeatWorkVO.setProjectName(projectVO.getSname());
        repeatWorkVO.setProjectCode(projectVO.getScode());
        return repeatWorkVO;
    }

    @Override
    public PageObject<Object> queryByCondition(String[] pkProject, String[] creators, String startDate, String endDate, int[] billStatus, int start, int size) throws BusinessException {
        int count = repeatWorkMapper.countByCondition(pkProject, creators, startDate, endDate, billStatus);
        RepeatWorkVO[] repeatWorkVOS = repeatWorkMapper.queryByCondition(pkProject, creators, startDate, endDate, billStatus, start, size);
        for (RepeatWorkVO vo : repeatWorkVOS) {
            //获取项目主键
            String pk = vo.getPkProject();
            ProjectVO projectVO = repeatWorkMapper.selectProject(pk);
            //赋值项目名
            vo.setProjectName(projectVO.getSname());
        }
        //获取图片？


        return new PageObject<Object>(repeatWorkVOS, count, start);
    }

    private  void getPics(RepeatWorkVO[] repeatWorkVOS) throws BusinessException{
        List<String> entityies = new ArrayList<>();
        for(RepeatWorkVO vo:repeatWorkVOS){
            entityies.add(vo.getPkRepeatwork());
        }
        Map<String, List<String>> entitiesForPic = picService.queryPicsByPkEntitys(entityies.toArray(new String[0]));
        for(RepeatWorkVO vo:repeatWorkVOS){
            List<String> pics = entitiesForPic.get(vo.getPkRepeatwork());
            if(null == pics || pics.size() == 0){
                continue;
            }
            vo.setPictures(pics.toArray(new String[0]));
            vo.setPicNum(pics.size());
        }

    }
}
